package com.efast.dmom.mgmt.controller.les.pda;

import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.dmom.mgmt.bean.les.GoodsReceiptBean;
import com.efast.dmom.mgmt.bean.les.PdaTwDocAsnDetailsBean;
import com.efast.dmom.mgmt.entity.les.TwDocAsnDetails;
import com.efast.dmom.mgmt.entity.les.TwDocAsnHeader;
import com.efast.dmom.mgmt.service.les.TwDocAsnDetailsService;
import com.efast.dmom.mgmt.service.les.TwDocAsnHeaderService;
import com.efast.dmom.mgmt.utils.Assist;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * PDA按跟踪号收货：查询数据的接口，确认收货的逻辑的修改
 *
 * @author ChenLiPeng
 */
@Controller
@RequestMapping(value = "/trackingNnumberReceiving")
@Api(description = "PDA按跟踪号收货")
public class TrackingNnumberReceivingController {

	private final TwDocAsnDetailsService twDocAsnDetailsService;
	private final TwDocAsnHeaderService twDocAsnHeaderService;

	@Autowired
	public TrackingNnumberReceivingController(TwDocAsnDetailsService twDocAsnDetailsService,
			TwDocAsnHeaderService twDocAsnHeaderService) {
		this.twDocAsnDetailsService = twDocAsnDetailsService;
		this.twDocAsnHeaderService = twDocAsnHeaderService;
	}

	@ApiOperation(value = "【根据零件包装流水号/跟踪号/托盘号查询数据】", httpMethod = "POST")
	@ResponseBody
	@RequestMapping(value = "/queryHis", method = RequestMethod.POST)
	public ResultBean queryHis(@RequestBody TwDocAsnDetails twDocAsnDetails) {
		ResultBean result = new ResultBean(true, "");
		Assist assist = new Assist();
		// 收货包装流水号 / 托盘号 /扫托盘的时候取这个值
		if (StringUtils.isNotBlank(twDocAsnDetails.getReceivePackSerial())) {
			assist.setRequires(Assist.andEq("receive_pack_serial", twDocAsnDetails.getReceivePackSerial()));
		}
		// 零件包装流水号 / 箱号 / 箱标签
		if (StringUtils.isNotBlank(twDocAsnDetails.getPartPackSerial())) {
			assist.setRequires(Assist.andEq("part_pack_serial", twDocAsnDetails.getPartPackSerial()));
		}
		if (StringUtils.isBlank(twDocAsnDetails.getReceivePackSerial())
				&& StringUtils.isBlank(twDocAsnDetails.getPartPackSerial())) {
			throw new ServiceException("请输入查询条件");
		}
		// 只显示新单的数据
		assist.setRequires(Assist.andEq("line_status", 0));
		List<TwDocAsnDetails> twDocAsnDetails1 = twDocAsnDetailsService.selectTwDocAsnDetails(assist);
		if (twDocAsnDetails1.size() == 0) {
			throw new ServiceException("没有查询到数据");
		}
		result.setObject(twDocAsnDetails1);
		return result;
	}

	@ApiOperation(value = "【pda确认收货】", httpMethod = "POST")
	@ResponseBody
	@RequestMapping(value = "/pdaGollectGoods", method = RequestMethod.POST)
	public ResultBean pdaGollectGoods(@RequestBody PdaTwDocAsnDetailsBean pdaTwDocAsnDetailsBean) {
		ResultBean result = new ResultBean(true, "");
		List<TwDocAsnDetails> pdaTwDocAsnDetailsBean1 = pdaTwDocAsnDetailsBean.getPdaTwDocAsnDetailsBean();
		if (pdaTwDocAsnDetailsBean1.size() == 0) {
			throw new ServiceException("pad确认收货记录为空");
		}
		// asn收货的时候托盘号，箱号，跟踪号可能为null，但是在pad收货中，全部不能为空
		for (TwDocAsnDetails aPdaTwDocAsnDetailsBean1 : pdaTwDocAsnDetailsBean1) {
			if (aPdaTwDocAsnDetailsBean1.getPartPackSerial() == null
					|| aPdaTwDocAsnDetailsBean1.getReceivePackSerial() == null
					|| aPdaTwDocAsnDetailsBean1.getInbSerialNo() == null) {
				throw new ServiceException(aPdaTwDocAsnDetailsBean1.getPartNoVersion() + "的箱号和托盘号已经库内跟踪号不能为空");
			}
		}
		TwDocAsnHeader twDocAsnHeader = twDocAsnHeaderService.selectTwDocAsnHeaderById(
				pdaTwDocAsnDetailsBean1.get(0).getTwDocAsnHeaderId());
		GoodsReceiptBean goodsReceiptBean = new GoodsReceiptBean();
		goodsReceiptBean.setTwDocAsnHeader(twDocAsnHeader);
		goodsReceiptBean.setListTwDocAsnDetails(pdaTwDocAsnDetailsBean1);
		twDocAsnHeaderService.headerReceive(goodsReceiptBean);
		return result;
	}

}
